<?php
/**
 * @author baby&star
 *
 *
 */
class UserController extends Controller{
	//TODO - Insert your code here
	function actionLogin(){
		if($_POST){
			$name = $_POST['name'];
			
			$pwd = $_POST['password'];
			
			$sno = $_POST['sno'];
			
			$VerifyCode = $this->createAction('captcha')->getVerifyCode();
			
			if(empty($name)){
				Common::jsalert("用户名不能为空!");
				exit;
			}
			if(empty($pwd)){
				Common::jsalerturl("密码不能为空!");
				exit;
			}
			
			if($VerifyCode != $sno){
				Common::jsalerturl("验证码错误！");
				exit;
			}
			
			$user = new User();
			$rs = $user->getById("`Name` = '".$name."' and `Password` = '".md5(md5($pwd))."'");
			
			if(empty($rs)){
				Common::jsalerturl("登录失败！");
				exit;
			}
			$_SESSION["userInfo"] = $rs;
			$this->redirect($this->createUrl("index/index"));
		}
		
		
		$this->renderPartial("login");
	}
	function actionLogout(){
		unset($_SESSION["userInfo"]);
		$this->redirect($this->createUrl("user/login"));
	}
	/**
	 * 新增
	 * Enter description here ...
	 */
	function actionAdd(){
		
		if($_POST){
			
			$name     = $_POST["name"];
			
			$pwd      = md5(md5($_POST["password"]));
			
			$DisplayName = $_POST["DisplayName"];
			
			$email    = $_POST["email"];
			
			//$userType = $_POST["userType"];
			
			$Mobile = $_POST["Mobile"];
			$phone = $_POST["Phone"];
			$LoginInToken = empty($_POST["LoginInToken"])?'0':$_POST["LoginInToken"];
			
			
			if(empty($name)){
				Common::jsalerturl("用户名不能为空！");	
			}
			if(empty($_POST["password"])){
				Common::jsalerturl("用户密码不能为空！");
			}
			if(empty($DisplayName)){
				Common::jsalerturl("用户显示用户名不能为空！");
			}
			if(!Common::matchEmail($email)){
				Common::jsalerturl("E-mail格式错误 ，请输入正确的E-mail地址！");
			}
			
			$arr = array("Name"=>$name,"Password"=>$pwd,"LoginInToken"=>$LoginInToken,"Mobile"=>$Mobile,"Phone"=>$phone,"DisplayName"=>$DisplayName,"Email"=>$email);
			$user  = new User();
			if($user->insertData($arr)){
				Common::jsalerturl("新增成功！！");
			}
			
		}
		$this->render("add");
	}
	/**
	 * 修改
	 */
	function actionModify(){
		
		if($_POST){
			
			$id       = $_POST["id"];
			
			$name     = $_POST["name"];
			
			$pwd      = md5(md5($_POST["password"]));
			
			$DisplayName = $_POST["DisplayName"];
			
			$email    = $_POST["email"];
			
			//$userType = $_POST["userType"];
			$Mobile = $_POST["Mobile"];
			$phone = $_POST["Phone"];
			$LoginInToken = empty($_POST["LoginInToken"])?'0':$_POST["LoginInToken"];
			if(empty($id)){
				Common::jsalerturl("非法操作，请重试！");
				exit;
			}
			
			if(empty($name)){
				Common::jsalerturl("用户名不能为空！");	
			}
			
			if(empty($DisplayName)){
				Common::jsalerturl("用户显示用户名不能为空！");
			}
			
			if(!Common::matchEmail($email)){
				Common::jsalerturl("E-mail格式错误 ，请输入正确的E-mail地址！");
			}
			
			$arr = array("Name"=>$name,"Password"=>$pwd,"LoginInToken"=>$LoginInToken,"Mobile"=>$Mobile,"Phone"=>$phone,"DisplayName"=>$DisplayName,"Email"=>$email);
			
			if(!empty($pwd)){
				$arr["Password"] = $pwd;
			}
			$user  = new User();
			
			$user->updateData($arr, "`UserID`='".$id."'");
				
			Common::jsalerturl("修改成功！！",$this->createUrl("user/userlist"));
				
			
		}
		
		$id = $_GET["id"];
		
		if(empty($id)){
			
			Common::jsalerturl("非法操作，请重试！");
			exit;
		}
		$user = new User();
		
		$info = $user->getById("`UserID`='".$id."'");
		
		
		$this->render("modify",array("info"=>$info));
	}
	/**
	 * 删除
	 */
	function actionDelete(){
		$id = $_GET['id'];
		if(!is_numeric($id)){
			Common::jsalerturl("非法操作，请重试！");
			exit;
		}
		$user = new User();
		$user->del("`UserID` = '".$id."'");
		Common::jsalerturl("删除成功！",$this->createUrl("user/userlist"));
		exit;
	}
	/**
	 * 用户列表
	 * Enter description here ...
	 */
	function actionUserlist(){
		
		$user = new User();
		$userInfo = $user->getList("","`UserID` desc",$_GET["page"]);
		
		
		$this->render("userlist",array("userInfo"=>$userInfo));
		//$this->renderPartial("userlist",array("userInfo"=>$userInfo));
	}
}
?>